package app.gt.screen.routerequest.map;

import java.io.IOException;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.gt.R;
import app.gt.screen.searchresult.SelectedRoute;
import app.gt.service.dialogs.AppDialogs;

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class SearchOnMap extends MapActivity implements OnClickListener {
	Geocoder geocoder = null;
	MapView mapView = null;
	List<Address> addressList = null;
	ProgressDialog progDialog = null;
	RelativeLayout EdittextShow;
	LinearLayout stringDEP;
	LinearLayout stringTEM;
	LinearLayout stringARR;
	int checkstringDEP = 0;
	int checkstringTEM = 0;
	int checkstringARR = 0;

	int markDEP = 0;
	int markTEM = 0;
	int markARR = 0;
	int numofOVer;

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.searchonmap);
		mapView = (MapView) findViewById(R.id.geoMap);
		mapView.setBuiltInZoomControls(true);
		mapView.getController().setZoom(10);
		geocoder = new Geocoder(this);
		setcentralpoint();
		addoverlay();
		initlayout();

	}

	private void initlayout() {
		// TODO Auto-generated method stub
		initpopupedittext();
	}

	private void initpopupedittext() {
		EdittextShow = (RelativeLayout) findViewById(R.id.routesearchmain);
		stringTEM = (LinearLayout) findViewById(R.id.routesearchstringTEM);
		EdittextShow.removeView(stringTEM);
		stringDEP = (LinearLayout) findViewById(R.id.routesearchstringDEP);
		EdittextShow.removeView(stringDEP);
		stringARR = (LinearLayout) findViewById(R.id.routesearchstringARR);
		EdittextShow.removeView(stringARR);
		TextView searchARRPlace = (TextView) findViewById(R.id.searchARRPlace);
		searchARRPlace.setOnClickListener(this);
		TextView searchTEMPlace = (TextView) findViewById(R.id.searchTEMPlace);
		searchTEMPlace.setOnClickListener(this);
		TextView searchDPPlace = (TextView) findViewById(R.id.searchDPPlace);
		searchDPPlace.setOnClickListener(this);
		TextView search = (TextView) findViewById(R.id.searchQuestion);
		search.setOnClickListener(this);
	}

	EditText locARR;

	private void InitEditTextArr() {
		// TODO Auto-generated method stub

		locARR = (EditText) findViewById(R.id.etARR);
		Button startARR = (Button) findViewById(R.id.geocodeBtnARR);
		startARR.setOnClickListener(this);

	}

	EditText locTEM;

	private void InitEditTextTem() {
		// TODO Auto-generated method stub

		locTEM = (EditText) findViewById(R.id.etTEM);
		Button startTEM = (Button) findViewById(R.id.geocodeBtnTEM);
		startTEM.setOnClickListener(this);

	}

	EditText locDEP;

	private void InitEditTextDep() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub

		locDEP = (EditText) findViewById(R.id.etDEP);
		Button startDEP = (Button) findViewById(R.id.geocodeBtnDEP);
		startDEP.setOnClickListener(this);

	}
	
	

	HelloItemizedOverlay itemizedoverlay;
	List<Overlay> listOfOverlays;

	private void addoverlay() {
		// TODO Auto-generated method stub
		// overlays
		MapOverlay mapOverlay = new MapOverlay();
		listOfOverlays = mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(mapOverlay);
		/*
		 * Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
		 * itemizedoverlay = new HelloItemizedOverlay(drawable); AddMarkers();
		 * listOfOverlays.add(itemizedoverlay);
		 */
		mapView.invalidate();
	}

	OverlayItem overlayitemDEP;
	OverlayItem overlayitemTEM;
	OverlayItem overlayitemARR;
	GeoPoint pointDEP;
	GeoPoint pointTEM;
	GeoPoint pointARR;
	private void AddMarkers(GeoPoint pt2, String locationNameDEP, int numofOVer2) {
		// TODO Auto-generated method stub
		Drawable marker = this.getResources().getDrawable(R.drawable.icon);
		marker.setBounds(0, 0, marker.getIntrinsicWidth(),
				marker.getIntrinsicHeight());
		itemizedoverlay = new HelloItemizedOverlay(marker);

		listOfOverlays.add(itemizedoverlay);

		if (numofOVer2 == 1) {
			pointDEP = new GeoPoint(pt2.getLatitudeE6(),
					pt2.getLongitudeE6());
			overlayitemDEP = new OverlayItem(pointDEP, "Hola, Mundo!",
					"I'm in Mexico City!");
			itemizedoverlay.addOverlay(overlayitemDEP);
		}

		if (numofOVer2 == 2) {
			pointTEM = new GeoPoint(pt2.getLatitudeE6(),
					pt2.getLongitudeE6());
			overlayitemTEM = new OverlayItem(pointTEM, "Sekai, konichiwa!",
					"I'm in Japan!");
			itemizedoverlay.addOverlay(overlayitemTEM);
		}

		if (numofOVer2 == 3) {
			 pointARR = new GeoPoint(pt2.getLatitudeE6(),
					pt2.getLongitudeE6());
			overlayitemARR = new OverlayItem(pointARR, "Sekai, konichiwa!",
					"I'm in Japan!");
			itemizedoverlay.addOverlay(overlayitemARR);
		}

	}

	private void setcentralpoint() {
		// TODO Auto-generated method stub
		// lat/long of Jacksonville, FL
		int lat = (int) (30.334954 * 1000000);
		int lng = (int) (-81.5625 * 1000000);
		GeoPoint pt = new GeoPoint(lat, lng);

		mapView.getController().setCenter(pt);
		mapView.getController().animateTo(pt);
	}

	public void doClick(String name) {

		progDialog = ProgressDialog.show(SearchOnMap.this, "Processing...",
				"Finding Location...", true, false);

		findLocation(name);

	}

	private void findLocation(final String locationName) {
		Thread thrd = new Thread() {
			public void run() {
				try {
					// do background work
					addressList = geocoder.getFromLocationName(locationName, 5);
					// send message to handler to process results
					uiCallback.sendEmptyMessage(0);

				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		thrd.start();

	}

	// ui thread callback handler
	private Handler uiCallback = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// tear down dialog
			progDialog.dismiss();

			if (addressList != null && addressList.size() > 0) {
				int lat = (int) (addressList.get(0).getLatitude() * 1000000);
				int lng = (int) (addressList.get(0).getLongitude() * 1000000);
				GeoPoint pt = new GeoPoint(lat, lng);
				mapView.getController().setZoom(15);

				mapView.getController().animateTo(pt);
				AddMarkers(pt, null, numofOVer);

			} else {
				Dialog foundNothingDlg = new AlertDialog.Builder(
						SearchOnMap.this).setIcon(0)
						.setTitle("Failed to Find Location")
						.setPositiveButton("Ok", null)
						.setMessage("Location Not Found...").create();
				foundNothingDlg.show();
			}
		}

	};

	private GeoPoint point;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.searchDPPlace:

			if (checkstringDEP == 0) {
				cleanView(v);
				EdittextShow.addView(stringDEP);
				InitEditTextDep();
				checkstringDEP = 1;
				if (pointDEP!= null) {
					mapView.getController().setCenter(pointDEP);
					mapView.getController().animateTo(pointDEP);
				}
			} else {
				cleanView(v);

			}

			break;
		case R.id.searchTEMPlace:

			if (checkstringTEM == 0) {
				cleanView(v);
				EdittextShow.addView(stringTEM);
				InitEditTextTem();
				checkstringTEM = 1;
				if (pointTEM!= null) {
					mapView.getController().setCenter(pointTEM);
					mapView.getController().animateTo(pointTEM);
				}
				
			} else {
				cleanView(v);

			}

			break;
		case R.id.searchARRPlace:

			if (checkstringARR == 0) {
				cleanView(v);
				EdittextShow.addView(stringARR);
				InitEditTextArr();
				checkstringARR = 1;
				if (pointARR!= null) {
					mapView.getController().setCenter(pointARR);
					mapView.getController().animateTo(pointARR);
				}
			} else {
				cleanView(v);

			}

			break;
		case R.id.geocodeBtnDEP:
			if (overlayitemDEP != null) {
				itemizedoverlay.removeOverlay(overlayitemDEP);
			}
			numofOVer = 1;
			String locationNameDEP = locDEP.getText().toString();
			doClick(locationNameDEP);

			break;
		case R.id.geocodeBtnTEM:
			if (overlayitemTEM != null) {
				itemizedoverlay.removeOverlay(overlayitemTEM);
			}
			numofOVer = 2;
			String locationNameTEM = locTEM.getText().toString();
			doClick(locationNameTEM);
			break;
		case R.id.geocodeBtnARR:
			if (overlayitemARR != null) {
				itemizedoverlay.removeOverlay(overlayitemARR);
			}
			numofOVer = 3;
			String locationNameARR = locARR.getText().toString();
			doClick(locationNameARR);
			break;
		
		case R.id.searchQuestion:
		Intent sharing = new Intent(this,SelectedRoute.class);
		startActivity(sharing);
		break;
	}// TODO Auto-generated method stub
	}

	private void cleanView(View v) {
		// TODO Auto-generated method stub
		EdittextShow.removeView(stringDEP);
		EdittextShow.removeView(stringTEM);
		EdittextShow.removeView(stringARR);
		checkstringDEP = 0;
		checkstringTEM = 0;
		checkstringARR = 0;
	}

}

class MapOverlay extends com.google.android.maps.Overlay {

	private long startTime = 0;
	private long endTime = 0;

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		return shadow;
		// ...
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {

		// ---when user lifts his finger---
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			startTime = event.getEventTime();
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			endTime = event.getEventTime();

		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			endTime = 1;
		}
		if (endTime - startTime > 1000) {
			GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
					(int) event.getY());
			AppDialogs.NotifyD(mapView.getContext(), "Hello");
			/*
			 * Toast.makeText(mapView.getContext(), p.getLatitudeE6() / 1E6 +
			 * "," + p.getLongitudeE6() / 1E6, Toast.LENGTH_SHORT).show();
			 */
		}
		return false;
	}
}
